package com.zhanbo.zhanbowymusic.main.featured;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.mwx.baselib.base.BaseBindingAdapter;
import com.mwx.baselib.base.BaseFragment;
import com.mwx.baselib.bean.ArtistSongListBean;
import com.mwx.baselib.bean.SingerListBean;
import com.mwx.baselib.bean.SongBean;
import com.mwx.baselib.net.BaseResponses;
import com.mwx.baselib.net.DefaultObserver;
import com.mwx.baselib.net.NetworkManager;
import com.mwx.baselib.utils.MLog;
import com.zhanbo.zhanbowymusic.R;
import com.zhanbo.zhanbowymusic.databinding.FragmentSingerSongListBinding;
import com.zhanbo.zhanbowymusic.main.mine.recently.adapter.SingleSongListAdapter;
import com.zhanbo.zhanbowymusic.music.MusicController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class SingerSongFragment extends BaseFragment<FragmentSingerSongListBinding> {
    private SingleSongListAdapter singleSongListAdapter;

    @Override
    protected Object getLayout() {
        MLog.d("R.layout.fragment_singer_song_list");
        return R.layout.fragment_singer_song_list;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        initView();
        initData();
        initListener();
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
    }

    @Override
    protected void initData() {
        if (getArguments()==null){
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put("artistId", getArguments().getInt("id")+"");
        NetworkManager.getInstance().getApiService().artistSongList(map)
                .subscribeOn(Schedulers.io())
                .compose(this.bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new DefaultObserver<BaseResponses<ArtistSongListBean>>(_mActivity) {
                    @Override
                    public void onSuccess(BaseResponses<ArtistSongListBean> listBaseResponses) {
                        singleSongListAdapter.getItems().clear();
                        singleSongListAdapter.getItems().addAll(listBaseResponses.getData().getItems());
                        singleSongListAdapter.notifyDataSetChanged();


                    }
                });
    }

    @Override
    protected void initView() {
        binding.recyclerSingerSong.setLayoutManager(new LinearLayoutManager(_mActivity,LinearLayoutManager.VERTICAL,false));
         singleSongListAdapter = new SingleSongListAdapter(_mActivity);
        binding.recyclerSingerSong.setAdapter(singleSongListAdapter);

    }

    @Override
    protected void initListener() {
        singleSongListAdapter.setOnItemClickListener(new BaseBindingAdapter.OnItemClickListener<SongBean>() {
            @Override
            public void onItemClick(int position) {
                MusicController.getInstance().updateItemDate(singleSongListAdapter.getItems().get(position));
            }
        });

    }

    public static SingerSongFragment newInstance(int singerId) {

        Bundle args = new Bundle();
        args.putInt("id",singerId);
        SingerSongFragment fragment = new SingerSongFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onDestroy() {
        MLog.d("SingerSongFragment---onDestroy");
        super.onDestroy();
    }

    public void playAll() {
        if (singleSongListAdapter.getItems()!=null) {
            MusicController.musicController.updateAndPlaySheetData(singleSongListAdapter.getItems());
        }
    }
}
